from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from common.Utils import BlogDriver

# 测试简墨登录界面
class BlogLogin:
    url = ""
    driver = ""
    wait = ""
    def __init__(self):
        self.url = "http://8.137.19.140:9090/blog_login.html"
        self.driver = BlogDriver.driver
        self.driver.get(self.url)
        self.driver.maximize_window()
        self.wait = WebDriverWait(self.driver, 3)

    # 成功登录的测试用例
    def LoginSucTest(self):
        # 每次输入之前都先进行清空操作，防止输入框已经有文字导致拼接字符串出现错误
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()

        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhangsan")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        # 能找到简墨首页用户昵称，则说明登录成功，否则登录失败
        self.wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "body > div.container > div.left > div > h3")))
        # 屏幕截图记录
        BlogDriver.getScreeShot()
        self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.left > div > h3")
        # 打印成功信息
        print("LoginSucTest:>"+self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.left > div > h3").text)
        # # 返回登录界面
        # self.driver.back()
    # 用户名错误的测试用例
    def LoginFailUserTest(self):
        # 每次输入之前都先进行清空操作，防止输入框已经有文字导致拼接字符串出现错误
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()

        # 错误账号
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhangsan_")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        # 检查是否登录失败
        self.wait.until(EC.alert_is_present())
        # 屏幕截图记录
        sleep(0.3)
        BlogDriver.getAlertScreenshot()
        # 出现弹窗，点击确定
        print("LoginFailUserTest:>"+self.driver.switch_to.alert.text)
        self.driver.switch_to.alert.accept()
    # 密码错误的测试用例
    def LoginFailPassTest(self):
        # 每次输入之前都先进行清空操作，防止输入框已经有文字导致拼接字符串出现错误
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()

        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhangsan")
        # 错误密码
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("12345")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        # 检查是否登录失败
        # 检查是否登录失败
        self.wait.until(EC.alert_is_present())
        # 屏幕截图记录
        sleep(0.3)
        BlogDriver.getAlertScreenshot()
        # 出现弹窗，点击确定
        print("LoginFailPassTest:>"+self.driver.switch_to.alert.text)
        self.driver.switch_to.alert.accept()
    # 用户名密码都错误的测试用例
    def LoginFailAllTest(self):
        # 每次输入之前都先进行清空操作，防止输入框已经有文字导致拼接字符串出现错误
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()

        # 错误账号和错误密码
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhangsan_")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("1234567")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        # 检查是否登录失败
        # 检查是否登录失败
        self.wait.until(EC.alert_is_present())
        # 屏幕截图记录
        sleep(0.3)
        BlogDriver.getAlertScreenshot()
        # 出现弹窗，点击确定
        print("LoginFailAllTest:>"+self.driver.switch_to.alert.text)
        self.driver.switch_to.alert.accept()

BlogLoginDriver = BlogLogin()
# BlogLoginDriver.LoginFailUserTest()
# BlogLoginDriver.LoginFailPassTest()
# BlogLoginDriver.LoginFailAllTest()
# BlogLoginDriver.LoginSucTest()
# BlogDriver.driverQuit()
